<template>
    <a-layout class="layout-container">
        <a-layout-sider class="layout-sider"
                        v-model:collapsed="collapsed">
            <div class="layout-logo">
                <img src="../assets/logo.png">
                <h1 v-show="!collapsed">${systemName}</h1>
            </div>
            <div class="layout-menu">
                <a-menu theme="dark"
                        mode="inline"
                        v-model:selectedKeys="selectedKeys"
                        :openKeys="openKeys"
                        @openChange="onOpenChange">
                    <template v-for="item in menus">
                        <a-sub-menu v-if="item.children" :key="item.path">
                            <template #icon>
                                <component :is="item.icon"></component>
                            </template>
                            <template #title>{{item.title}}</template>
                            <a-menu-item v-for="child in item.children" :key="child.path">
                                <router-link :to="child.path">
                                    <span>{{child.title}}</span>
                                </router-link>
                            </a-menu-item>
                        </a-sub-menu>
                        <a-menu-item v-else :key="item.path">
                            <template #icon>
                                <component :is="item.icon"></component>
                            </template>
                            <router-link :to="item.path">
                                <span>{{item.title}}</span>
                            </router-link>
                        </a-menu-item>
                    </template>
                </a-menu>
            </div>
        </a-layout-sider>
        <a-layout class="layout-main">
            <a-layout-header class="layout-header">
                <a-space :size="20">
                    <div class="trigger" @click="() => (collapsed = !collapsed)">
                        <menu-unfold-outlined v-if="collapsed"/>
                        <menu-fold-outlined v-else/>
                    </div>
                    <a-breadcrumb style="width: 160px">
                        <a-breadcrumb-item v-for="(item, index) in route.matched" :key="index">
                            {{item.meta.title}}
                        </a-breadcrumb-item>
                    </a-breadcrumb>
                </a-space>
                <a-space>
                    <a href="#">
                        <question-circle-outlined/>
                        文档
                    </a>
                    <a-dropdown placement="bottomCenter">
                        <span class="avatar">
                            <a-avatar size="small">
                                <template #icon>
                                  <user-outlined />
                                </template>
                            </a-avatar>
                            {{userInfo?.userName}}
                        </span>
                        <template #overlay>
                            <a-menu>
                                <a-menu-item>
                                    <user-outlined/>
                                    个人中心
                                </a-menu-item>
                                <a-menu-divider/>
                                <a-menu-item @click.prevent="doLogout">
                                    <poweroff-outlined/>
                                    退出登录
                                </a-menu-item>
                            </a-menu>
                        </template>
                    </a-dropdown>
                </a-space>
            </a-layout-header>
            <a-layout-content class="layout-content">
                <router-view></router-view>
            </a-layout-content>
        </a-layout>
    </a-layout>
</template>

<script>
    import {defineComponent, reactive, toRefs, onMounted} from 'vue';
    import {useRouter, useRoute} from 'vue-router';
    import {useStore} from '@/store';
    // import WebApi from "@/webapi/WebApi";

    export default defineComponent({
        components: {
        },
        setup() {
            const state = reactive({
                openKeys: [],
                selectedKeys: [],
                collapsed: false,
                menus: [],
                userInfo: useStore().userInfo
            });

            const router = useRouter();
            const route = useRoute();

            state.openKeys = [route.matched[0].path];
            state.selectedKeys = [route.matched[1].path];

            onMounted(() => {
                if (!state.userInfo) {
                    //this.$router.push('/login');
                    return;
                }
            });

            state.menus = router.options.routes.filter(item => !item.meta.hidden)
                .map((item) => {
                    if (item.meta.leaf) {
                        return {
                            path: item.path,
                            title: item.meta.title,
                            icon: item.meta.icon
                        };
                    } else {
                        const children = item.children.filter(child => !child.meta.hidden)
                            .map((child) => {
                                return {
                                    path: child.path,
                                    title: child.meta.title,
                                    icon: child.meta.icon
                                };
                            });
                        return {
                            path: item.path,
                            title: item.meta.title,
                            icon: item.meta.icon,
                            children: children
                        };
                    }
                });

            const onOpenChange = (openKeys) => {
                const rootSubmenuKeys = state.menus.map(menu => menu.path);
                const latestOpenKey = openKeys.find(key => state.openKeys.indexOf(key) === -1);
                if (rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
                    state.openKeys = openKeys;
                } else {
                    state.openKeys = latestOpenKey ? [latestOpenKey] : [];
                }
            };

            return {
                ...toRefs(state),
                router,
                route,
                onOpenChange
            };
        },
    });
</script>

<style lang="less" scoped>
@header-height: 50px;

    .layout-container {
        .layout-sider {
            .layout-logo {
                display: flex;
                align-items: center;
                height: @header-height;
                line-height: @header-height;
                padding-left: 24px;
                white-space: nowrap;

                img {
                    height: 32px;
                    margin-right: 4px;
                }

                h1 {
                    margin: 0;
                    overflow: hidden;
                    color: #fff;
                    font-weight: 600;
                    font-size: 18px;
                    line-height: 32px;
                }
            }

            .layout-menu {
                height: calc(100vh - @header-height);
                overflow-y: auto;
                overflow-x: hidden;
            }
        }

        .layout-main {
            .layout-header {
                width: 100%;
                height: @header-height;
                line-height: @header-height;
                background: #fff;
                z-index: 1;
                padding: 0 20px;
                display: flex;
                justify-content: space-between;
                align-items: center;
                box-shadow: 0 1px 4px rgb(0 21 41 / 8%);

                .trigger {
                    display: flex;
                    align-items: center;
                    margin-left: 8px;
                    font-size: 18px;
                }

                .trigger:hover {
                    color: var(--antd-wave-shadow-color);
                    cursor: pointer;
                }

                .avatar {
                    display: inline-block;
                    padding: 0 12px;

                    .ant-avatar {
                        margin: 13px 4px;
                        vertical-align: top;
                    }

                    &:hover {
                        cursor: pointer;
                        background: rgba(0,0,0,.05);
                    }
                }
            }

            .layout-content {
                height: calc(100vh - @header-height);
                overflow-y: auto;
                overflow-x: hidden;
                background-color: #f3f3f3;
                padding: 8px;
            }
        }
    }
</style>
